<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>update</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

</head>
<body>
<div class="container">
    <form role="form" id="ddd" class="form-horizontal">
        <div class="form-group">
            <label for="firstname" class="col-sm-2 control-label">id</label>
            <div class="col-sm-10">
                <input type="text" readonly class="form-control" v-model="arr.id" name="id">
            </div>
        </div>

        <div class="form-group">
            <label for="firstname" class="col-sm-2 control-label">名字</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" v-model="arr.name" name="name">
            </div>
        </div>

        <div class="form-group">
            <label for="firstname" class="col-sm-2 control-label">工作</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" v-model="arr.job"  name="job" >
            </div>
        </div>

        <div class="form-group">
            <label for="firstname" class="col-sm-2 control-label">工资</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" v-model="arr.sal" name="sal">
            </div>
        </div>
        <input class="btn btn-primary" type="button" value="修改" @click="update()">
    </form>
</div>
<!--引入axios框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!--引入vue框架-->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<!--sweetalert-->
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

<script>
    let v = new Vue({
        el:"form",
        data:{
            arr:[],
        },
        methods:{
            update:function () {
                let data = new FormData(document.querySelector("form"));
                // console.log(data);
                axios.post("/update",data).then(function (response) {
                    //返回1修改成功,返回0修改失败
                    if (response.data == 1){
                        swal({
                            title: "修改成功!",
                            text: "3秒钟后跳转到列表页面!",
                            icon: "success",
                            button: "关闭",
                            timer: 3000,
                        });
                        setTimeout("location.href='/list.html'",3000)
                        // location.href="/list.html";
                        v.info="修改成功!"
                    }else {
                        swal({
                            title: "修改失败!",
                            text: "3秒钟后关闭!",
                            icon: "error",
                            button: "关闭",
                            timer: 3000,
                        });
                        v.info="修改失败!"
                    }
                })
            }
        },
        created:function () {//创建时执行
            let id = location.search.split("=")[1];
            axios.get("/selectbyid?id="+id).then(function (response) {
                //服务器返回的内容实际上是Json格式的字符串，axios框架内部会将此字符串
                // 转成数组装对象的格式│
                v.arr = response.data;
            })
        }
    })
</script>
</body>
</html>